package com.niodata.dp.core.usermgr.dao;

import com.niodata.dp.core.usermgr.enums.GroupRole;
import com.niodata.dp.core.usermgr.model.UserGroup;
import com.niodata.dp.core.usermgr.model.UserGroupInfo;
import java.util.List;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Options;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

/**
 * Created by ndp on 17-3-8.
 */
@Mapper
public interface UserGroupMapper {

  @Options(useGeneratedKeys = true, keyProperty = "userGroup.groupId", keyColumn = "GROUP_ID")
  void insert(@Param("userGroup") UserGroup userGroup);

  void delete(@Param("userId") Long userId, @Param("groupId") Long groupId);

  void deleteUserGroups(@Param("userId") Long userId);

  List<UserGroup> getUserGroupList(@Param("groupId") Long groupId, @Param("role") GroupRole role);

  UserGroup getMaster(@Param("groupId") Long groupId);

  UserGroup getUserGroup(@Param("userId") Long userId, @Param("groupId") Long groupId);

  List<UserGroup> getGroupRoleList(@Param("userId") Long userId, @Param("active") boolean active);

  List<UserGroup> getMasterGroups(@Param("userId") Long userId);

  void updateUserGroup(@Param("userGroup") UserGroup userGroup);

  /**
   * 根据用户的ID删除全部的用户分组信息.
   *
   * @param userId 需要删除的用户主键ID
   */
  void deleteByUserId(@Param("userId") Long userId);

  /**
   * 根据用户的ID和指定的组角色级别，查询对应的分组信息.
   *
   * @param userId 用户ID
   * @param role 组角色
   * @param active 0无效，1有效
   * @return 查询的组信息
   */
  List<UserGroup> getUserGroupInfoByRole(@Param("userId") Long userId,
        @Param("role") GroupRole role, @Param("active") boolean active);

  @Select("select ug.USER_ID as userId,ug.GROUP_ID as groupId,"
        + "u.USER_NAME as userName,g.GROUP_NAME as groupName,"
        + "ug.ROLE as role,ug.CREATE_TIME as createTime"
        + "  from USER_GROUP ug,GROUP_INFO g,USER_INFO u "
        + "where u.USER_ID=ug.USER_ID and ug.GROUP_ID=#{groupId} and g.GROUP_ID=#{groupId}")
  List<UserGroupInfo> getUserGroupInfoOfGroup(@Param("groupId") Long groupId);

}
